|
| Contents | |
| About This Extension |
Integrating sonification of a Web page's user interface with background music can be a tricky thing. Especially challenging is the job of tying chromatic (pitched) user interface sounds to the specific key of background music. Well-chosen background music can create a strong soundtrack for a Web site that really enhances the user experience, and it would be a shame to spoil such a soundtrack with inappropriately chosen musical notes and chords.
The methods provided in this extension make it easier to apply a musical knowledge of background music to the sonification of user interface elements. With methods such as playChord, one can easily play chords that blend in harmoniously with the background music, simply by specifying a chord name. The extension allows music-savvy Web developers to apply their knowledge of music in a manner that is familiar to them, proving invaluable as an aid in the development of Web-based music instruction applications and intelligent musical "jam along" applications.
| What's New in the Latest Version |
There is a dedicated version history for this extension, where you will be able to find out about the new features and possible issues with the most recent version. The version history for this extension is independent of the version history for the core Music Object API.
| Getting This Extension |
To get the "music-object-x-chords.js" JavaScript file that implements this extension, simply right-click on this link (click-and-hold for Mac users). Then, from the menu that pops up, select the 'Save Link As...' or 'Save Target As...' option and save the file as "music-object-x-chords.js" into an appropriate folder of the local copy of your Web site.
In case you have problems saving the file using the above link, the "music-object-x-chords.js" file can also be found inside the documentation's "javascript" folder, along with the "music-object.js" file.
| Dependencies |
In order to use this extension, the following JavaScript file(s) must first be sourced in...
For more information on using extensions, consult the section About Music Object Extensions.
| Reference |
| |||||||||||||
| Instance Methods |
| playChord |
|
The playChord method plays a musical chord, as specified by a chord name parameter and other parameters.
|
playChord ( MidiChannel1to16 , BankNumber0to2 , ProgramNumber0to255 , ChordNameSTR , OctaveNumber0to12 , ChordRootNoteNameSTR , Velocity0to127 , NoteDurationMsINT ) |
When using this method, as well as the Music.getChordNotes static method, chord names must be expressed using different combinations of the elements listed below. The elements must appear in the chord name in the order in which they are listed and should not be separated by spaces, or the chord name may not be interpreted correctly. All the elements are optional, with the exception of the chord root which is required.
Chord Name Elements (listed in the order in which they should appear)
To get a better understanding of how these elements can be combined in different ways to create different chords, see the demo for the Chords extension.
The playChord method has the following limitations...
When no NoteDurationMsINT parameter is specified, then the default value of 1000 (1 second) will be used for this parameter.
When there is also no Velocity0to127 parameter specified, then the default value of 127 will be used for this parameter.
When there is also no ChordRootNoteNameSTR parameter specified, then the chord root that is specified in the ChordNameSTR parameter will be used and will not be overridden.
When there is also no OctaveNumber0to12 parameter specified, then the default value of 0 will be used for this parameter.
| playNotes |
|
The playNotes method plays a set of any number of musical notes, as specified by a notes array parameter.
|
playNotes ( MidiChannel1to16 , BankNumber0to2 , ProgramNumber0to255 , NotesARRAY , Velocity0to127 , NoteDurationMsINT ) |
The playNotes method will play all of the notes contained in the NotesARRAY parameter on the specified MIDI channel. All notes will be played using the same instrument, at the same velocity, and for the same duration, as specified by the BankNumber0to2, ProgramNumber0to255, Velocity0to127, and NoteDurationMsINT parameters.
The elements of the notes array, specified by the NotesARRAY parameter, may be either note names or note numbers. All elements that are not of type "string" or "number" will be ignored. This includes elements that have a value of null, and also elements that are of type "boolean". The fact that null-value elements are ignored by this method is particularly important because the array that is returned by the Music.getChordNotes static method will invariably contain some null-value elements.
The playNotes method is useful as a way of playing an arbitrarily large number of notes simultaneously, since there is no restriction on the number of notes that can be specified by the NotesARRAY parameter. The method is also useful for playing notes that don't conform to any chord that could otherwise be played using the playChord method, or interchangeably playing chord note arrays produced by the Music.getChordNotes static method and note arrays that are generated using a custom function or constructed manually.
The playChord and Music.getChordNotes methods take three parameters to fully qualify the notes that make up a chord: ChordNameSTR, OctaveNumber0to12, and ChordRootNoteNameSTR. Because of this, it isn't possible to call these methods with a single parameter that can represent an exact chord. So, if for example you wanted to design code that iterated in some pattern through an array of chords, then the playNotes method would come in very handy.
In the following code example, the function playNextChord will step through the chords in the array named "chords". When it reaches the end of the "chords" array it will being again from the first element.
chordNo = -1;
function playNextChord () {
chordNo = (chordNo + 1) % chords.length;
myMusicObject.playNotes (1,1,11,chords [chordNo],127,2000);
}
chords = new Array (
Music.getChordNotes ('Eb7#9',3,'Eb'),
'C#3,C#4,E#4,Db5,C#5,Gb5',
Music.getChordNotes ('A7#11',3,'A'),
'C3,D#4,F4,G#4,Bb5',
Music.getChordNotes ('Emaj9',3,'E'),
new Array ('C#4','F#4','G#4','B4','D#5'),
new Array (49,63,65,68,72,75),
Music.getChordNotes ('Abmin11',3,'Ab'),
);
myMusicObject = new Music ();
myMusicObject.stubEmbed ('stub.rmf');
|
From the listing above, you will notice what may seem like quite a bit of "hanky-panky" going on with the creation of the "chords" array. Along with several elements that are being created using the Music.getChordNotes method, there are also some elements being declared as simple strings, as well as some other elements being created using the new operator. Because the playNotes method can take either a NotesARRAY parameter or a NoteNamesSTR parameter, it doesn't truly matter that all the elements of the "chords" array are not of the same type. When the playNextChord function selects element 0 of the "chords" array, it will be passing an array reference to the playNotes method. But, when the function selects element 1, it will pass the string value "
The playNotes method is, indeed, quite flexible. In the sample code listing below, all the calls to the playNotes method will play exactly the same chord...
myMusicObject.playNotes (1,1,11,'C#3,D#4,F4,G#4,C5',127,2000);
myMusicObject.playNotes (1,1,11,new Array (49,63,65,68,72),127,2000);
myMusicObject.playNotes (1,1,11,new Array ('C#3','D#4','F4','G#4','C5'),127,2000);
myMusicObject.playNotes (1,1,11,new Array (49,'D#4','F4',68,72),127,2000);
myNotesArray = new Array (49,63,65,68,72);
myMusicObject.playNotes (1,1,11,myNotesArray,127,2000);
|
Now, let's take a look at the calls to the playNotes method above...
When a NoteNamesSTR parameter is used in place of a NotesARRAY parameter, then a string containing a comma-separated list of note names can be used to specify the notes that should be played.
When no NoteDurationMsINT parameter is specified, then the default value of 1000 (1 second) will be used for this parameter.
When there is also no Velocity0to127 parameter specified, then the default value of 127 will be used for this parameter.
| Static Methods |
| Music.getChordNotes |
|
The Music.getChordNotes static method returns an array, representing the musical notes that make up a specified chord.
| Music.getChordNotes ( ChordNameSTR , OctaveNumber0to12 , ChordRootNoteNameSTR ) |
| NoteNumbersARRAY |
The array that is returned by the Music.getChordNotes method will contain exactly 14 elements, with element 0 always containing a null value, and the elements 1 through 13 containing either note numbers or null values. Elements 1 through 13 represent the notes that make up a chord. For example, element 1 will represent the chord root note, element 2 will represent the chord's 2nd note, element 3 will represent the chord's 3rd note, and so on. If the chord does not contain a certain note, then the element corresponding to that chord note will be set to a null value. For instance, if a particular chord does not contain a 7th note, then element 7 will be set to null. On the other hand, if the chord does contain a 7th note, then element 7 will contain the MIDI note number of the chord's 7th note.
The following table shows several example chords with a listing of the MIDI note numbers that make up the chords. In the first three columns are the chord name, octave, and root note that determine the notes of the chord, and in the remaining 13 columns are the values of elements 1 through 13 of the array that will be returned by the Music.getChordNotes method.
|
The playChord method takes three parameters to fully qualify the notes that make up a chord: ChordNameSTR, OctaveNumber0to12, and ChordRootNoteNameSTR. Because of this, it isn't possible to call this method with a single parameter that can represent an exact chord. So, if for example you wanted to design code to play a randomly selected chord from an array of chords, then you would be better off using the Music.getChordNotes method in conjunction with the playNotes method instead of the playChord method.
In the following code example, the function playRandomChord will randomly select a chord from the array named "chords"...
function playRandomChord () {
var chordNo = Math.floor (Math.random () * chords.length);
myMusicObject.playNotes (1,1,11,chords [chordNo],127,2000);
}
chords = new Array (
Music.getChordNotes ('Eb7#9',3,'Eb'),
Music.getChordNotes ('A7#11',3,'A'),
Music.getChordNotes ('Emaj9',3,'E'),
Music.getChordNotes ('Dbsus9',4,'Db'),
Music.getChordNotes ('Abmin11',3,'Ab'),
Music.getChordNotes ('Emaj7#11',4,'E'),
Music.getChordNotes ('Bb7#5#9',3,'Bb'),
Music.getChordNotes ('Eb7#9',3,'Eb'),
Music.getChordNotes ('A7#11',3,'A')
);
myMusicObject = new Music ();
myMusicObject.stubEmbed ('stub.rmf');
|
In this example, the array of chords is created using the new constructor and is assigned to the variable "chords". Don't mind the fact that the statement is broken up across multiple lines - this is a perfectly acceptable thing to do in JavaScript, provided that you end each line on a logical token and you don't ever have any unterminated string literals. Each element of the array "chords" is, itself, an array containing the MIDI note numbers of the notes that make up a chord. The array for each element is created using a call to the Music.getChordNotes method. Now, when the playRandomChord function selects a chord, it will pass an element of the array "chords" by reference to the playNotes method, and the playNotes method will in turn play the notes of the chord.
The array that is returned by the Music.getChordNotes method is likely to contain some null-value elements. If, for the purposes of your own code, you needed a compacted array of notes containing no null-value elements, then you could write a custom function as follows...
function compactChord (chordName,chordOctave,chordRoot) {
var
chordNotes = Music.getChordNotes (chordName,chordOctave,chordRoot),
compactNotes = new Array ()
;
for (var noteNo = 0; noteNo < chordNotes.length; noteNo++) {
if (chordNotes [noteNo] != null) {
compactNotes [compactNotes.length] = chordNotes [noteNo];
}
}
return compactNotes;
}
|
For detailed guidelines on the chord name syntax that should be used for this method, consult the reference for the playChord method.
When no ChordRootNoteNameSTR parameter is specified, then the chord root that is specified in the ChordNameSTR parameter will be used and will not be overridden.
When there is also no OctaveNumber0to12 parameter specified, then the default value of 0 will be used for this parameter.